// pages/bindphone/bindphone.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    messCode:"",
    hasBindPhone: false,
    userid:"",
    time: '获取验证码', //倒计时 
    currentTime: 60,
    disabled:false
  },
  getInfo() {
    let that = this;
    network.requestGet(host + '/xcx/customer/getUserInfo', {}, res => {
      console.log("res1", res)
      if (res.data.code == 200) {
        that.setData({ 
          userid:res.data.data.id
        })
        if (res.data.data.phone) {
          that.setData({
            phone: res.data.data.phone,
            hasBindPhone: true,
          })
          console.log("userID="+this.data.userid)
        }


      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  getPhoneNumber(e) {
    console.log(e)
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      
      
      that.setData({
        phone: res.result.event.weRunData.data.phoneNumber 
      })
      that.updateIphoneData(res.result.event.weRunData.data.phoneNumber)
    }).catch(err => {
      console.error(err);
    });
  },
  updateIphoneData(e){
    
    let that = this;
    network.requestPost(host + '/xcx/customer/update-customer', {
      "id":that.data.userid,
      "phone":that.data.phone
    }, res => {
      console.log("editCustomer=", res)
      if (res.data.code == 200) {
        that.setData({
          hasBindPhone: true 
        })
        wx.showToast({
          title: '绑定成功',
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
   // 发送验证码
  getMessageCodeData(e){
    
    let that = this;
    if(!that.data.phone){
      wx.showToast({
        title: '请填写手机号',
      })
      return;
    }
    that.getCode(); 
    that.setData({
      disabled:true
    })
     
    network.requestGet(host + '/getSmsCode', {
      "type":0,
      "phonenumber":that.data.phone
    }, res => {
      console.log("editCustomer=", res)
      if (res.data.code == 200) { 
        wx.showToast({
          title: '验证码发送成功',
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  getCode: function (options){
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime+'秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime:60,
          disabled: false   
        })
      }
    }, 1000)  
  },
  // 绑定手机号
  bindIphoneData(e){
    
    let that = this;
    if(!that.data.phone){
      wx.showToast({
        title: '请填写手机号',
      })
      return;
    }
    if(!that.data.messCode){
        wx.showToast({
          title: '请填写验证码',
        })
        return;
    }
    network.requestPost(host + '/xcx/customer/bindPhone', {
      "code":that.data.messCode,
      "phone":that.data.phone
    }, res => {
      console.log("editCustomer=", res)
      if (res.data.code == 200) {
        that.setData({
          hasBindPhone: true 
        })
        wx.showToast({
          title: '绑定成功',
        })
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  phoneChange(e){
    this.setData({
      phone:e.detail.value
    }) 
    
  },
  messCodeChange(e){
    
    this.setData({
      messCode:e.detail.value
    }) 
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})